import { ACTION_TYPES } from 'utils/constant';
import _ from 'lodash';
import moment from 'moment';
import socket from 'utils/socket';
import { Get } from 'utils/handler';
import { getId } from 'utils/tools';

export const receiveMsg = () => (dispatch, getState) => {
    

    socket.on('receivemsg', function(data) {
        const linkmans = getState().chat.linkmans;
        const { id } = data;
        const linkmanMessage = [].concat(linkmans[id] || [], {...data});

        dispatch({
            type: ACTION_TYPES.UPDATE_SELF_MESSAGE,
            messages: {
                [id]: linkmanMessage
            }
            
        })
       
    })
}



export const sendMsg = data => dispatch => {
    socket.emit('sendmsg', { ...data });
}


export const getMsg = id => (dispatch, getState) => {
    dispatch({
        type: ACTION_TYPES.MSG_LIST_REQUEST
    })
    Get('/message/list', {
        data: {
            id
        }
    }).then(res => {
        if (res.reasonCode === 200) {
            dispatch({
                type: ACTION_TYPES.MSG_LIST_SUCCESS,
                // data: res.data,
                chatName: res.username
            })
        } else {
            dispatch({
                type: ACTION_TYPES.MSG_LIST_FAIL
            })
        }
    }).catch(e => {
        dispatch({
            type: ACTION_TYPES.MSG_LIST_FAIL
        })
    })
}

export const getMsgList = id => (dispatch, getState) => {
    dispatch({
        type: ACTION_TYPES.MSG_LIST_SUCCESS
    })
    
}



export const receiveChatList = id => (dispatch, getState) => {
    const messages = getState().chat.allMsgs;
    const validMessages = messages.filter(item => item.to === id || item.from === id); // 个人
    const chats = _.groupBy(validMessages, 'id');
    dispatch({
        type: ACTION_TYPES.CHAT_LIST_SUCCESS,
        linkmas: chats
    })
}


export const getLinkmansLastMessage = () => async (dispatch, getState) => {
    const { friends = [], groups = [] } = getState().login.user;
    friends.forEach(friend => {
        Object.assign(friend, {
            _id: getId(friend.to._id, friend.from._id)
        })
    })
    const linkmansId = [
        ...friends.map(g => g._id),
        ...groups.map(g => g._id)
    ]
    await Get('/message/linkmansLastMessage', {
        data: {
            linkmansId: linkmansId.join(',')
        }
    }).then(res => {
        if (res.reasonCode === 200) {
            dispatch({
                type: ACTION_TYPES.LINKMANS_MESSAGE,
                messages: res.messages
            })
        }
    }).catch(e => {

    })
}

export const getLinkmanHistoryMessages = (id, existCount) => (dispatch, getState) => {
    Get('/message/linkmanHistoryMessage', {
        data: {
            id, existCount
        }
    }).then(res => {
        const messages = res.messages.sort((a, b) => moment(a.create_time).valueOf() - moment(b.create_time).valueOf());
        dispatch({
            type: ACTION_TYPES.ADD_LINKMAN_MESSAGE,
            messages: {
                [id]: messages
            }
        })
    })
}